package com.it2297.controller;

import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.it2297.main.CompanyApp;
import com.it2297.model.Company;
import com.it2297.model.Needy;
//import com.it2297.view.CompanyHomeScreen;
import com.it2297.view.CompanyLogin;
import com.it2297.view.CompanyLogin;
import com.it2297.view.CompanyCreateCompany;
import com.it2297.view.CompanyRecoverPassword;
import com.it2297.view.CompanyViewDetails;
import com.it2297.view.JobSearchJob;
import com.it2297.view.CompanyUpdateCompany;
import com.it2297.view.CompanyViewCompanyDetails;


public class CompanyController {
	
	private CompanyCreateCompany view;
	private CompanyUpdateCompany view2;
	private CompanyLogin view3;
	private CompanyViewCompanyDetails view4;
	//private CompanyHomeScreen view5;
	private CompanyRecoverPassword view6;
	private CompanyViewDetails view7;
	
	public CompanyController(CompanyCreateCompany ccv){
		this.view = ccv;
	}
	
	public CompanyController(CompanyUpdateCompany ucv){
		this.view2=ucv;
	}
	
	public CompanyController(CompanyLogin cl){
		this.view3=cl;
	}
	
	public CompanyController(CompanyViewCompanyDetails vct){
		this.view4=vct;
	}
/*	public CompanyController(CompanyHomeScreen chs){
		this.view5=chs;
	}
	*/
	public CompanyController(CompanyRecoverPassword crp){
		this.view6=crp;
	}
	public CompanyController(CompanyViewDetails cvd){
		this.view7=cvd;
	}
	
	public void CreateCompany(){
		Company company= new Company();
		company.setCompanyName(view.getName());
		company.setPassword(view.getPassword());
		company.setEmail(view.getEmail());
		company.setAddress(view.getAddress());
		company.setPhone(view.getPhone());
		company.setDescription(view.getDescription());
		company.setRecoveryQuestion(view.getRecoveryQuestion());
		company.setRecoveryAnswer(view.getRecoveryAnswer());

		 if(Company.IsCompanyExist(company.getCompanyName())!=true){
				
				Company.createCompany(company);
				JOptionPane.showMessageDialog(null, "Company created successfully!");
		 }
		 else{
			 JOptionPane.showMessageDialog(null, "Similar company name found, unable to register.");
		 }
	}
	

	public ArrayList<String> retrieveCompanyName(){
		Company company = new Company();
		return company.retrieveCompanyName();
		}
	
	
	public void UpdateCompany(){
			Company company= new Company();
			company.setCompanyName(view2.getCompanyName());
			company.setPassword(view2.getPassword());
			company.setEmail(view2.getEmail());
			company.setAddress(view2.getAddress());
			company.setPhone(view2.getPhone());
			company.setDescription(view2.getDescription());
			company.setRecoveryQuestion(view2.getRecoveryQuestion());
			company.setRecoveryAnswer(view2.getRecoveryAnswer());
			
			
			Company.updateDetails(company);
			JOptionPane.showMessageDialog(null, "Record updated successfully!");
			
			Company company2=Company.retrieveDetails(view2.getCompanyName());
			CompanyApp.setCurrentUser(company2);
	}
	
	
	
	public void retrieveFullCompanyDetails(){
		Company company = Company.retrieveDetails(view2.getCompanyName());
		
		view2.setPassword(company.getPassword());
		view2.setEmail(company.getEmail());
		view2.setPhone(Integer.toString(company.getPhone()));
		view2.setAddress(company.getAddress());
		view2.setDescription(company.getDescription());
		view2.setRecoveryQuestion(company.getRecoveryQuestion());
		view2.setRecoveryAnswer(company.getRecoveryAnswer());
		
	}
	

	public void validateLogin(){
		if(Company.IsCompanyExist(view3.getCompanyName())){
			
			Company company=Company.retrieveDetails(view3.getCompanyName());

				if(company.getCompanyName().equals(view3.getCompanyName()) && company.getPassword().equals(view3.getPassword())){
					CompanyApp.setCurrentUser(company);
				}
			
				else
				{
					JOptionPane.showMessageDialog(null, "Invalid username or password!");			
				}	
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Invalid username or password!");
		}
	}
	
	public void viewCompanyDetails(){
		Company company = Company.retrieveDetails(view4.getCompanyName());
		
		view4.setEmail(company.getEmail());
		view4.setPhone(Integer.toString(company.getPhone()));
		view4.setAddress(company.getAddress());
		view4.setDescription(company.getDescription());
	}
	
	
	public void getRecoveryQuestion(){
		if(Company.IsCompanyExist(view6.getCompanyName())){
			Company company = Company.retrieveDetails(view6.getCompanyName());
			view6.setRecoveryQuestion(company.getRecoveryQuestion());
			view6.setRecoveryAnswer("");
		}
		else{
			JOptionPane.showMessageDialog(null, "Company does not exist!");
			view6.setRecoveryQuestion("");
			view6.setRecoveryAnswer("");
			view6.setPassword("");
		}

	}
	
	public void validateRecoveryPassword(){	
		if(Company.IsCompanyExist(view6.getCompanyName())){
			Company company=Company.retrieveDetails(view6.getCompanyName());
				if(company.getRecoveryQuestion().equals(view6.getRecoveryQuestion()) && company.getRecoveryAnswer().equals(view6.getRecoveryAnswer())){
					view6.setPassword(company.getPassword());
				}		
				else
				{
					JOptionPane.showMessageDialog(null, "Invalid recovery question and answer combination!");	
					view6.setPassword("");
				}			
		}
		else{
			JOptionPane.showMessageDialog(null, "Company does not exist!");
		}
	}
	
	public void refresh(){
		Company company=Company.retrieveDetails(view7.getName());
		CompanyApp.setCurrentUser(company);
		
	}
	
	
	
}
